package com.nginxmanager.controller;

import com.nginxmanager.domain.Conf;
import com.nginxmanager.domain.Proxy;
import com.nginxmanager.server.ConfRepository;
import com.nginxmanager.server.config.nginxConf.AllNginxConf;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-01-11.
 */
@RestController
@RequestMapping("/nginx")
@Api(tags = "Nginx性能配置项Api")
public class ConfController {

    @Autowired
    ConfRepository confRepository;
    private AllNginxConf nginxConf = new AllNginxConf();
    private Conf conf = new Conf();

    @GetMapping("/findConf")
    @ApiOperation("查看指定配置项")
    public ResponseEntity<List<String>> find(@RequestParam("address")String address,@RequestParam("key")String key ){
        List<String> list = new ArrayList<String>();
        conf.setKey(key);
        try {
            list= nginxConf.get(address,conf);
        }catch (Exception e){
          return ResponseEntity.status(412).body(null);
        }
        return new ResponseEntity<List<String>>(list, HttpStatus.valueOf(200));
    }


    @PostMapping("/addConf")
    @ApiOperation("添加一项配置项")
    public ResponseEntity<String> add(@RequestParam("address")String address,@RequestBody Conf conf1){
        List<String> list = new ArrayList<String>();
        list = nginxConf.get(address,conf1);
        if (!list.isEmpty()){
            try {
                Conf conf2 = confRepository.findOne(conf1.getId());
                conf2.setFlag(true);
                confRepository.save(conf2);
            }catch (Exception e){
                return ResponseEntity.status(412).body("配置项已经存在"+list.toString());
            }
            return ResponseEntity.status(412).body("配置项已经存在"+list.toString());
        }
        try {
            Conf conf2 = confRepository.findOne(conf1.getId());
            conf1.setAllConf(conf2.getAllConf());
            conf1.setConsult(conf2.getConsult());
            conf1.setModel(conf2.getModel());
            nginxConf.add(address,conf1);
            conf2.setFlag(true);
            confRepository.save(conf1);
        }catch (Exception e){
            return  new ResponseEntity<String>(conf1.toString(), HttpStatus.valueOf(201));
        }
        return  new ResponseEntity<String>(conf1.toString(), HttpStatus.valueOf(200));
    }

    @PutMapping("/putConf")
    @ApiOperation("修改配置项")
    public ResponseEntity<String> upt(@RequestParam("address")String address,@RequestBody Conf conf1){
        if (conf1.getId()==null){
            List<String> list  = nginxConf.get(address,conf1);
            if(!list.isEmpty()){
                nginxConf.upt(address,conf1);
                return ResponseEntity.status(200).body("配置文件成功");
            }
            nginxConf.add(address,conf1);
            return ResponseEntity.status(200).body("配置文件成功");
        }
        List<String> list  = nginxConf.get(address,conf1);
        //List<String> list = confRepository.finOne(str,a);
           Conf conf2 = confRepository.findOne(conf1.getId());
           conf1.setAllConf(conf2.getAllConf());
            conf1.setConsult(conf2.getConsult());
            conf1.setModel(conf2.getModel());
        if (list.isEmpty()){
            confRepository.save(conf1);
            return ResponseEntity.status(200).body("修改数据库成功");
        }
        try {
            nginxConf.upt(address,conf1);
            confRepository.save(conf1);
        }catch (Exception e){
            return  new ResponseEntity<String>(conf1.toString(), HttpStatus.valueOf(201));
        }
        return  new ResponseEntity<String>(conf1.toString(), HttpStatus.valueOf(200));
    }

    @DeleteMapping("/delConf")
    @ApiOperation("删除配置项")
    public ResponseEntity<String> deleda(@RequestParam("address")String address,@RequestBody Conf conf1){
        try {
            Conf conf2 = confRepository.findOne(conf1.getId());
            conf1.setAllConf(conf2.getAllConf());
            conf1.setConsult(conf2.getConsult());
            conf1.setModel(conf2.getModel());
            nginxConf.delete(address,conf1);
            conf1.setFlag(false);
            confRepository.save(conf1);
        }catch (Exception e){
            return ResponseEntity.status(412).body(null);
        }
        return  new ResponseEntity<String>(conf1.toString(), HttpStatus.valueOf(200));
    }


}
